package com.aipanda.entity;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * 广告类
 * 
 * @param title 标题
 * @param type 广告类型
 * @param content 内容
 * @param path 路径
 * @param beginDate 开始时间
 * @param endDate 结束时间
 * @param url URL	链接地址
 * @param adPosition 广告位
 * @author wanyuxiang
 * 
 */
@Entity
@Table(name = "aipanda_ad")
public class Ad extends OrderEntity {
	private static final long serialVersionUID = -1307743303786909390L;
	private String title;
	private Type type;
	private String content;
	private String path;
	private Date beginDate;
	private Date endDate;
	private String url;
	private AdPosition adPosition;

	/**
	 * 广告类型类
	 * @param text 文本
	 * @param image 图片
	 * @param  flash flash播放
	 * @author wanyuxiang
	 *
	 */
	public enum Type {
		text, image, flash;
	}

	@NotEmpty
	@Length(max = 255)
	@Column(nullable = false)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@NotNull
	@Column(nullable = false)
	public Type getType() {
		return this.type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	@Lob
	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Length(max = 255)
	public String getPath() {
		return this.path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	@Column(name="begin_date")
	public Date getBeginDate() {
		return this.beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}
	@Column(name="end_date")
	public Date getEndDate() {
		return this.endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	@Length(max = 255)
	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="ad_position",nullable = false)
	public AdPosition getAdPosition() {
		return this.adPosition;
	}

	public void setAdPosition(AdPosition adPosition) {
		this.adPosition = adPosition;
	}

	@Transient
	public boolean hasBegun() {
		return getBeginDate() == null || new Date().after(getBeginDate());
	}

	@Transient
	public boolean hasEnded() {
		return (getEndDate() != null) && (new Date().after(getEndDate()));
	}

}
